﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using PddHelper.Framework;
using System;

namespace WebApiExtend
{
    /// <summary>
    /// 
    /// </summary>
    public class Base : Attribute
    {
        internal LogModel InitLogModel(HttpContext httpContext)
        {
            var obj = new LogModel()
            {
                starttime = DateTime.Now,
                requestraw = $"{httpContext.Request.Path}{httpContext.Request.QueryString}",
                domain = $"{httpContext.Request.Scheme}://{httpContext.Request.Host}",

                //ltctraid = httpContext.Request.Headers["x-ltctraid"].ToString(),
                //requestpid = httpContext.Request.Headers["x-requestpid"].ToString(),
                //requestid = httpContext.Request.Headers["x-requestid"].ToString(),
            };
            return obj;
        }

        internal void LogInfo(LogModel entity)
        {
            entity.endtime = DateTime.Now;
            var iso = new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss fff" };
            _ = new LogHelper().Info(JsonConvert.SerializeObject(entity, iso));
        }
        internal void LogError(string str)
        {
            _ = new LogHelper().Error(str);
        }
    }
}
